define([
    'initTerrain', 'initMapBox',
    'scriptLoader'
], function (initTerrain, initMapBox, scriptLoader) {
    return ['$rootScope', '$scope', '$timeout', function ($rootScope, $scope, $timeout) {

//================================================================================
//region Map Inits
//================================================================================

        window.s = $scope;

        $scope.isTerrainActive = true;

        $scope.toggleTerrainActive = function () {
            if (bmap.setTerrain) {
                if (bmap.style.terrain) {
                    bmap.setTerrain();
                } else {
                    bmap.setTerrain({'source': 'mapbox-dem', 'exaggeration': 2});
                }
                $timeout(function () {
                    $scope.isTerrainActive = !$scope.isTerrainActive;
                });
            } else {
                if (window.terrainObject) {
                    window.terrainObject.toggle($scope.isTerrainActive);
                }
            }
        };

        function simplify(n) {
            var frac = n - ~~n;
            var digits = ~~n;
            if ((frac + '').length > 3 && (digits + '').length > 1) {
                return ~~(n * 1e3) / 1e3;
            }
            return n;
        }

        $scope.bmap = null;

        function getParameterByName(name, url) {
            if (!url) url = window.location.href;
            name = name.replace(/[\[\]]/g, "\\$&");
            var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                results = regex.exec(url);
            if (!results) return null;
            if (!results[2]) return '';
            return decodeURIComponent(results[2].replace(/\+/g, " "));
        }

        function isPositiveParameter(name) {
            var param = getParameterByName(name);
            if (param) {
                if (param == 0 || param == '0' || param == 'false' || param == 'no') {
                    return false;
                }
                return true;
            }
            return false;
        }

        initMapBox(function (bmap, gl) {

            $scope.bmap = bmap;
            initTerrain(bmap, gl);

            scriptLoader.loadHistoryEvent('EXAMPLE_HISTORY_EVENT', function (event) {
                scriptLoader.renderHistoryEvent(event, bmap, gl);
            });

        });


//================================================================================
//endregion
//================================================================================

    }];
});